package base

/**
  * Created by 焕滨 on 2016/5/30.
  */
object JSONParseDemo {

  def main(args: Array[String]) {
    val str =
      """{"_ip":"172.17.32.27","_time":"2016-05-17 20:40:40.438","action":"CUSTOM_APP_MENU_DATA","app":{"appClass":3,"appCreater":"415093","appDesc":"    包           ","appId":"FSAID_131473c","appMode":1,"appName":"    ","appType":1,"createrType":1,"gmtCreate":1463457182000,"gmtModified":1463457182000,"payType":2,"properties":"{\"detailImageUrl\":[],\"createTemplateId\":\"2a5720a499854fbeac8a674762f3ba9c\"}","serviceName":"    ","status":1},"fsEa":"415093","menuNum":3,"menuNunFirst":3,"menuNunSecond":4,"menuNunThird":4,"userId":1000}""".stripMargin
    import scala.util.parsing.json._
    JSON.globalNumberParser = {input:String => input.toLong}
    JSON.perThreadNumberParser = {input:String => input.toLong}
//    val jsonMap = JSON.parseFull(str).get.asInstanceOf[Map[String,Any]]

    //使用parseRaw解析,解析出来只有的JSONObject和JSONArray两种类型，JSONObject对应Map，JSONArray对应List
    val jsonRaw = JSON.parseRaw(str).get
    //将解析结果转化成map类型
    val jsonMap = jsonRaw.asInstanceOf[JSONObject].obj
    val ip = jsonMap("_ip")
    val app = jsonMap("app").asInstanceOf[JSONObject].obj
    //json对象重新转化成string对象
    val jsonStr = jsonRaw.toString()
    println(ip)
    println(app)
    println(jsonStr)
    val jsonObj = new JSONObject(jsonMap)
    println(jsonObj)

  }
}
